package com.aabte.permissionframeworklearn.permission.controller;

import com.aabte.permissionframeworklearn.permission.beans.PageQuery;
import com.aabte.permissionframeworklearn.permission.beans.PageResult;
import com.aabte.permissionframeworklearn.permission.common.ResponseBean;
import com.aabte.permissionframeworklearn.permission.model.SysAcl;
import com.aabte.permissionframeworklearn.permission.model.SysRole;
import com.aabte.permissionframeworklearn.permission.param.AclParam;
import com.aabte.permissionframeworklearn.permission.service.SysAclService;
import com.aabte.permissionframeworklearn.permission.service.SysRoleService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Daniel
 */
@Controller
@RequestMapping("/sys/acl")
public class SysAclController {

    @Autowired
    private SysAclService sysAclService;
    @Autowired
    private SysRoleService sysRoleService;

    @PostMapping("/save.json")
    @ResponseBody
    public ResponseBean saveAcl(
            @RequestBody AclParam param) {
        SysAcl save = sysAclService.save(param);
        return ResponseBean.success(save);
    }

    @PutMapping("/update.json")
    @ResponseBody
    public ResponseBean updateAcl(
            @RequestBody AclParam param) {
        SysAcl update = sysAclService.update(param);
        return ResponseBean.success(update);
    }

    @GetMapping("/page.json")
    @ResponseBody
    public ResponseBean listAcl(
            @RequestParam Long aclModuleId,
            PageQuery pageQuery) {
        PageResult<SysAcl> pageResult = sysAclService.getPageByAclModuleId(aclModuleId, pageQuery);
        return ResponseBean.success(pageResult);
    }

    @GetMapping("/acls.json")
    @ResponseBody
    public ResponseBean acls(
            @RequestParam(value = "aclId") Long aclId
    ) {
        List<SysRole> roleList = sysRoleService.getRoleListByAclId(aclId);
        Map<String, Object> map = Maps.newHashMap();
        map.put("users", sysRoleService.getUserListByRoles(roleList));
        map.put("roles", roleList);
        return ResponseBean.success(map);
    }
}
